package com.yy.weixinpay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Ordering;
import com.google.gson.Gson;
import com.yy.weixinpay.entity.OrderInfo;
import com.yy.weixinpay.entity.PaymentInfo;
import com.yy.weixinpay.entity.Product;
import com.yy.weixinpay.enums.OrderStatus;
import com.yy.weixinpay.enums.PayType;
import com.yy.weixinpay.mapper.OrderInfoMapper;
import com.yy.weixinpay.mapper.ProductMapper;
import com.yy.weixinpay.service.OrderInfoService;
import com.yy.weixinpay.utils.OrderNoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
    @Autowired
    private ProductMapper productMapper;
    @Override
    public OrderInfo createOrderByProductId(Long productId,String paymentType) {
        //防止重复下单，查找已经存在，但是未支付的订单
        //如果存在，直接返回订单信息,后面代码不在执行
        OrderInfo order = this.getPayOrderByProductId(productId,paymentType);
        if (order != null){
            return  order;
        }
        //根据productId获取商品信息
        Product product = productMapper.selectById(productId);
        //生成订单
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setTitle(product.getTitle());
        orderInfo.setOrderNo(OrderNoUtils.getOrderNo());
        orderInfo.setProductId(productId);
        orderInfo.setTotalFee(product.getPrice());
        orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());
        orderInfo.setPaymentType(paymentType);
        //把订单信息保存到数据库
        baseMapper.insert(orderInfo);
        return orderInfo;
    }

    /**
     * 支付的二维码放入数据库中
     * @param orderNo
     * @param codeUrl
     */
    @Override
    public void saveCodeUrl(String orderNo, String codeUrl) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCodeUrl(codeUrl);
        baseMapper.update(orderInfo,queryWrapper);
    }

    @Override
    public List<OrderInfo> listOrderByCreateTimeDesc() {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 更新订单状态
     * @param out_trade_no
     * @param orderStatus
     */
    @Override
    public void updateStatusByOrderNo(String out_trade_no, OrderStatus orderStatus) {
        log.info("支付状态:{}",orderStatus.getType());
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderStatus(orderStatus.getType());
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",out_trade_no);
        baseMapper.update(orderInfo,queryWrapper);
    }

    /**
     * 查看订单更新状态
     * @param out_trade_no
     * @return
     */
    @Override
    public String getOrderStatus(String out_trade_no) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",out_trade_no);
        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
        if (orderInfo == null){
            return null;
        }
        return orderInfo.getOrderStatus();
    }

    @Override
    public List<OrderInfo> getNoPayOrderByDuration(int minutes,String paymentType) {
        //获取五分钟之前的实例
        Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_status",OrderStatus.NOTPAY.getType());
        queryWrapper.le("create_time",instant);
        queryWrapper.eq("payment_type",paymentType);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public OrderInfo getOrderByOrderNo(String orderNo) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        return baseMapper.selectOne(queryWrapper);
    }


    private OrderInfo getPayOrderByProductId(Long productId,String paymentType) {
        //查询已存在，但是未支付的订单
        //按照条件查询，封装条件
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",productId);
        queryWrapper.eq("order_status",OrderStatus.NOTPAY.getType());
        queryWrapper.eq("payment_type",paymentType);
        return baseMapper.selectOne(queryWrapper);
    }
}
